home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 December / PCWDEC06.iso / Software / Trial / Paint Shop Pro XI / Data1.cab / parser.py < prev    next >
Encoding:
Python Source  |  2006-08-04  |  5.1 KB  |  174 lines

  1. # runs and matches lines.  sticks parameters into a tuple.  some formatting issues in tuple.  parm name
  2. # put in with leading blanks.  Could put in using match group to fix maybe...
  3.  
  4. # does not handle commented psp commands
  5.  
  6. true = 1
  7. false = 0
  8. MaxLineLength = 85
  9.  
  10. runInPSP = true
  11. debug = false
  12.  
  13. if not runInPSP:
  14.     Environment = {'key': 1}
  15.  
  16. if runInPSP:
  17.     from PSPApp import *
  18.  
  19. import sys, re, string, types, os, traceback
  20.  
  21. import locale        # we need to specifically set the locale to C since the OS setting
  22.                     # may override the default locale
  23.  
  24. from PSPTools import *
  25. import PSPScriptMsgs
  26. import PSPScriptPyBlocks
  27.  
  28. from PSPScriptParse import *
  29.  
  30. # display exception info
  31. def PrintException():
  32.     excType, excVal, excTraceBack = sys.exc_info()
  33.     print str(excType)
  34.     print str(excVal)
  35.     #print str(excTraceBack)
  36.     traceback.print_exc(file=sys.stdout)
  37.     
  38. #******************************************************************************
  39. #   Function:    Convert()
  40. #   Author:      Steve Neumeyer
  41. #   Purpose:     Convert result from old parser code into something we can parse in
  42. #                 the Python/C API once this gets into PSP
  43. #   Returns:     a list of dictionaries that contain various keys describing what kind of
  44. #                 block has been parsed and its attributes
  45. #   Parameters:  the output from the parser, and the 'Envrionment' dictionary
  46. #   Notes:       
  47. #******************************************************************************
  48. def Convert(sp, Environment):
  49.     struct = []
  50.  
  51.     propertiesBlock = false
  52.  
  53.     # if no script properties block, add one
  54.     for pyBlock in sp.pyBlocks:
  55.         if pyBlock.commandType == 'Script Properties':
  56.             propertiesBlock = true
  57.  
  58.     if propertiesBlock == false:
  59.         return None
  60.  
  61.     for pyBlock in sp.pyBlocks:
  62.         Dict = {}
  63.         Dict['type'] = pyBlock.commandType
  64.         # print Dict['type']
  65.  
  66.         # need to add a cr for command and commented command types        
  67. #        if Dict['type'] == "PSPCmd" or Dict['type'] == "CmtedPSPCmd":
  68. #            Dict['text'] = pyBlock.block + '\n'
  69. #        else:
  70. #            Dict['text'] = pyBlock.block
  71.         Dict['text'] = pyBlock.block
  72.  
  73.         if Dict['type'] == "PSPCmd" or Dict['type'] == "CmtedPSPCmd":
  74.             if Dict['type'] == "CmtedPSPCmd":
  75.                 Dict['PR'] = GetDictFromCmdString( RemoveComments(pyBlock.block) )
  76.             else:
  77.                 Dict['PR'] = GetDictFromCmdString( pyBlock.block )
  78.         elif Dict['type'] == "Script Properties":
  79.             Dict['PR'] = GetPropertiesDict( pyBlock.block )
  80.         else:
  81.             Dict['PR'] = {}
  82.             
  83.         if pyBlock.commandType == 'PSPCmd' or pyBlock.commandType == 'CmtedPSPCmd':
  84.             if pyBlock.commandType == 'CmtedPSPCmd':
  85.                 commandinfo = GetQuotedFields( RemoveComments(pyBlock.block) )
  86.             else:                
  87.                 commandinfo = GetQuotedFields( pyBlock.block )
  88.             if runInPSP:
  89.                 AllCmdInfo = App.Do(Environment, 'GetCommandInfo', {
  90.                     'TargetCmd': commandinfo[0], 
  91.                     'GeneralSettings': {
  92.                         'ExecutionMode': App.Constants.ExecutionMode.Default
  93.                     }, 
  94.                     'ParamInfo': App.Constants.ParamInfo.None
  95.                     })
  96.                 Dict['localCommandName'] = AllCmdInfo['LocalName']
  97.                 editable = 0
  98.                 for x in AllCmdInfo['ResourceAttributes']:
  99.                     if x[0] == 'IsEditable':
  100.                         editable = x[1]
  101.                 if editable != 0:
  102.                     Dict['editable'] = 'Editable'
  103.                 else:
  104.                     Dict['editable'] = 'NOTEditable'
  105.             else:
  106.                 Dict['editable'] = "NOTEditable"
  107.                 Dict['localCommandName'] = ""
  108.             Dict['commandName'] = commandinfo[0]
  109.         else:
  110.             Dict['editable'] = "NOTEditable"
  111.             Dict['localCommandName'] = ""
  112.             Dict['commandName'] = ""
  113.             
  114.         struct += [Dict]
  115.  
  116.     return struct    
  117.     
  118. #******************************************************************************
  119. #   Function:    Do()
  120. #   Author:      Carl Lestor
  121. #   Purpose:     Script main loop. Get a script name from the user, parse it,
  122. #                  create a GUI from it, then run the Tk mainloop()
  123. #   Returns:     
  124. #   Parameters:  
  125. #   Notes:       
  126. #******************************************************************************
  127. def Do(Environment):
  128.     # set the locale to C (the default) so that eval() will work properly with real numbers
  129.     locale.setlocale(locale.LC_NUMERIC, 'C')
  130.     
  131.     # construct instance
  132.     sp = ParseScript()
  133.     userFileName = ""
  134.     val = None
  135.  
  136.     if Environment.has_key('__scriptpath__') and runInPSP:
  137.         # print 'ENVIRONMENT DICT'
  138.         # print Environment['__scriptpath__']
  139.         userFileName = Environment['__scriptpath__']        
  140.     else:
  141.         print 'ERROR: CmdEditScript did not specify a filename'
  142.  
  143.     # print 'USER FILE NAME'
  144.     # print userFileName
  145.  
  146.     sp, parseResult = DoParse(userFileName, sp)
  147.  
  148.     if (parseResult[0]):
  149.         #print "SP"
  150.         #print sp
  151.         #print 'parse result'
  152.         #print parseResult
  153.         # print sp.errorString
  154.  
  155.         try:
  156.             val = Convert(sp, Environment)
  157.         except (NameError, SyntaxError, IndexError):
  158.             if debug:
  159.                 print "Unexpected Error: ", sys.exc_info()[0]
  160.                 print "Trackback: \n", traceback.print_tb(sys.exc_info()[2])
  161.             val = None
  162.         except:
  163.             print "Unexpected Error: ", sys.exc_info()[0]
  164.             print "Trackback: \n", traceback.print_tb(sys.exc_info()[2])
  165.         return val
  166.     else: # parse failed
  167.         return None 
  168.  
  169.     #return Convert(sp, Environment)
  170.  
  171. # Main function, run when invoked as a stand-alone Python program.
  172. if __name__ == '__main__':
  173.     Do(Environment)
  174.